Skip to main content

πŸ”€ Unit 2: Data Types, Variables & Constants

Welcome back, coder-in-training! πŸ‘‹
In this unit, we’ll dive into the building blocks of C β€” data types, variables, and constants.
Think of these as the alphabet of programming: once you master them, you can form meaningful β€œsentences” (programs). ✨


πŸ“¦ Data Types in C​

Data types tell the computer what kind of data you want to store.

πŸ— Primary Data Types​

Data TypeKeywordSize (bytes)RangeFormat Specifier
Characterchar1-128 to 127%c
Integerint4-2,147,483,648 to 2,147,483,647%d or %i
Floatfloat43.4E-38 to 3.4E+38%f
Doubledouble81.7E-308 to 1.7E+308%lf
Voidvoid0No value-

πŸ›  Data Type Modifiers​

Modifiers tweak the size and range of basic data types:

ModifierUsed WithPurpose
shortintReduces size (2 bytes)
longint, doubleIncreases size
signedint, charAllows negatives (default)
unsignedint, charOnly positive values
short int a;           // 2 bytes: -32,768 to 32,767
long int b; // 8 bytes: larger range
unsigned int c; // 0 to 4,294,967,295
unsigned char d; // 0 to 255
long long int e; // Very large numbers

🏷 Variables​

A variable is like a box πŸ“¦ with a label β€” you can store a value and change it later.

Declaration Syntax​

datatype variable_name;

// Examples
int age;
float salary;
char grade;

Initialization Styles​

int age;
age = 25;

Naming Rules βœ…βŒβ€‹

βœ… Must start with a letter or underscore
βœ… Case sensitive (age, Age, AGE are different)
❌ No spaces, no special symbols, no keywords

// Good
int studentAge;
float total_salary;
char GRADE;
int _privateVar;

// Bad
int a;
int x1234;
int myVariableForStoringTheAgeOfStudent;

πŸ—Ώ Constants​

Constants are values that cannot change once set. Like your date of birth πŸŽ‚.

Types of Constants​

πŸ”’ Literals​

10, -50, 0x1A, 077   // Integers
3.14, -0.5, 2.5e3 // Floating
'A', '5', '\n' // Characters
"Hello" // Strings

πŸ”– Symbolic Constants​

#define PI 3.14159
#define MAX_SIZE 100
#define NEWLINE '\n'

🎹 Escape Sequences​

SequenceMeaning
\nNew line
\tTab
\\Backslash
\"Double quote
\'Single quote
\0Null terminator
\bBackspace
\rCarriage return

βž• Operators​

Operators are the tools πŸ”§ that let you work with values.

Arithmetic Operators​

OperatorExampleMeaning
+a + bAdd
-a - bSubtract
*a * bMultiply
/a / bDivide
%a % bRemainder
int result1 = 10 / 3; // Result = 3

Relational Operators​

OperatorExampleMeaning
==a == bEqual
!=a != bNot equal
>a > bGreater
<a < bLess
>=a >= bGreater/Equal
<=a <= bLess/Equal

Logical Operators​

| Operator | Example | True If... | | -------- | --------------------- | --------------- | -------- | --- | --------- | ----------------- | | && | (a > 5) && (b < 10) | Both true | | | | | (a > 5) | | (b < 10) | At least one true | | ! | !(a > 5) | Reverses result |


Assignment Operators​

OperatorExampleEquivalent
=a = 5Assign
+=a += 3a = a + 3
-=a -= 3a = a - 3
*=a *= 3a = a * 3
/=a /= 3a = a / 3
%=a %= 3a = a % 3

Increment & Decrement​

int a = 5, b;
b = ++a; // a = 6, b = 6

Conditional (Ternary)​

status = (age >= 18) ? "Adult" : "Minor";
int max = (a > b) ? a : b;

πŸ”„ Type Conversion​

int a = 10;
float b = 5.5;
float result = a + b; // a converted to float

πŸŽ“ Complete Example​

#include <stdio.h>
#define PI 3.14159

int main() {
const int MAX_MARKS = 100;
int marks = 85;
float percentage;
char grade;

percentage = (float)marks / MAX_MARKS * 100;

grade = (percentage >= 90) ? 'A' :
(percentage >= 80) ? 'B' :
(percentage >= 70) ? 'C' :
(percentage >= 60) ? 'D' : 'F';

printf("=== Student Report Card ===\n");
printf("Marks: %d/%d\n", marks, MAX_MARKS);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);

if (marks >= 40 && marks <= MAX_MARKS) {
printf("Status: PASS\n");
} else {
printf("Status: FAIL\n");
}

int bonus = 5;
marks += bonus;
printf("After bonus: %d\n", marks);

return 0;
}

πŸ“ Quick Quiz​

What is the difference between pre-increment and post-increment?

  • No difference
  • Pre-increment increases before using, post-increment after βœ